package com.liu.frame.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.liu.frame.R;
import com.liu.frame.adapter.MessageAdapter;
import com.liu.frame.adapter.ProductAdapter;
import com.liu.frame.application.MyApplication;
import com.liu.frame.base.BaseActivity;
import com.liu.frame.base.BaseRecyclerViewAdapter;
import com.liu.frame.beans.MessageBean;
import com.liu.frame.beans.ProductBean;
import com.liu.frame.beans.UserBean;
import com.liu.frame.config.Api;
import com.liu.frame.customview.PullTorefreshView;
import com.liu.frame.http.WeiLiCaiNetWorks;
import com.liu.frame.http.WeiLiCaiOkHttp;
import com.liu.frame.utils.EventBusUtils;
import com.liu.frame.utils.LogUtils;
import com.liu.frame.utils.SharedPreferencesUtils;
import com.liu.frame.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by 刘智强 on 2017/2/14.
 */

public class MeActivity extends BaseActivity implements PullTorefreshView.PullToRefrehListtener {
    //    private PtrClassicFrameLayout ptrClassicFrameLayout;
//    private RecyclerView recyclerView;
    private ProductAdapter productAdapter;
    private MessageAdapter messageAdapter;
    private List<MessageBean.MessageCenterBean> datas = new ArrayList<>();
    private List<ProductBean> productBeanList = new ArrayList<>();
    private PullTorefreshView pull_to_refresh_view;
    private PtrClassicFrameLayout ptrClassicFrameLayout;//刷新的控件
    private RecyclerView recyclerView;//列表控件
    private PullTorefreshView<MessageBean.MessageCenterBean> refrshview;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_me);

    }

    @Override
    public void setTitleView() {
        myTitleBar=$(R.id.titlebar);
        myTitleBar.setTitle("我的");
        myTitleBar.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initView() {
//        ptrClassicFrameLayout=$(R.id.ptr_framelayout);
//        recyclerView=$(R.id.rv_product);
//        pull_to_refresh_view = $(R.id.pull_to_refresh_view);
//        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_framelayout);
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        refrshview = $(R.id.refrshview);
        refrshview.setLayoutManager(0, 0);


    }

    @Override
    public void initEvents() {
        refrshview.setPullToRefrehListtener(this);
    }

    @Override
    public void objectLogic() {
        messageAdapter = new MessageAdapter(mActThis, datas, R.layout.product_item);
//        productAdapter = new ProductAdapter(mActThis, productBeanList, R.layout.product_item);
//        getMessage(1);
//        getProduct();
        getMessage(1);
    }

    @Override
    public void onClickEvent(View v) {

    }

    @Override
    public void loadCache() {
//        try {
//            datas = new SharedPreferencesUtils(mActThis).getList(Api.PRODUCT);
//            if (datas!=null){
//                productAdapter = new ProductAdapter(mActThis, datas, R.layout.product_item);
//                setDatas(datas);
//            }
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

    }

    public void getMessage(int page) {
        String user_id = MyApplication.getInstance().getmUser().getUser_id();
        parametersMap.put("User_id", user_id);
        parametersMap.put("page", page + "");
        WeiLiCaiNetWorks<MessageBean> netWorks = new WeiLiCaiNetWorks<MessageBean>(mActThis) {
        };
        subscription = netWorks.post(service.getMessage(parameters.postAllParameters(parametersMap)), new WeiLiCaiNetWorks.CallBack<MessageBean>() {
            @Override
            public void start() {

            }

            @Override
            public void completed() {

            }

            @Override
            public void error() {

            }

            @Override
            public void fail(String code, String message, String returnInfo) {

            }

            @Override
            public void success(String code, String message, MessageBean messageBean) {
                LogUtils.e("1111->" + messageBean.getMessage().size());
                if (messageBean != null && !messageBean.getMessage().isEmpty()) {
                    refrshview.setData(messageAdapter,messageBean.getMessage());
                } else {
                    ToastUtils.myToast(mActThis, "已经没有更多数据了");
                }

                refrshview.refreshComplete();
            }
        });
    }


    public void getProduct() {
        WeiLiCaiNetWorks<ProductBean> netWorks = new WeiLiCaiNetWorks<ProductBean>(mActThis) {
        };
        subscription= netWorks.post(service.getProduct(parameters.postAllParameters(parametersMap)),
                new WeiLiCaiNetWorks.CallBack<List<ProductBean>>() {
                    @Override
                    public void start() {

                    }

                    @Override
                    public void completed() {

                    }

                    @Override
                    public void error() {

                    }

                    @Override
                    public void fail(String code, String message, String returnInfo) {

                    }

                    @Override
                    public void success(String code, String message, List<ProductBean> productBeanList) {
                        if (!productBeanList.isEmpty()) {
//                            refrshview.setData(productAdapter, productBeanList);
                        }
//                        datas.addAll(productBeanList);
//                        setDatas(datas);

//                        try {
//                            new SharedPreferencesUtils(mActThis).putList(Api.PRODUCT, productBeanList);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        for (ProductBean p : productBeanList
//                                ) {
//                            LogUtils.e(p.getName());
//
//                        }

                    }
                });
    }

    @Override
    public void refresh() {

        getMessage(1);

    }

    @Override
    public void loadMore(int page) {
        LogUtils.e("页码->" + page);
        getMessage(page);

    }

    /**
     * 重新加载
     */

    @Override
    public void retryLoadData() {
        getMessage(1);
    }

    @Override
    public void onItemClickListener(View v, int position) {
        ToastUtils.myToast(mActThis, messageAdapter.getDatas().get(position).getTitile());

    }

}
